//
//  RadioCollectionViewCell.m
//  inquiry
//
//  Created by 高宇 on 2020/5/23.
//  Copyright © 2020 高宇. All rights reserved.
//

#import "RadioCollectionViewCell.h"
#import "GXToast.h"

@implementation RadioCollectionViewCell

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        NSString *xibName = NSStringFromClass([RadioCollectionViewCell class]);
        self = [[NSBundle mainBundle] loadNibNamed:xibName owner:self options:nil].firstObject;
    }
    return self;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    
}

- (IBAction)topClick:(id)sender {
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];  //数据都存在这个字典里
//    if (_lastValue) {
//        NSMutableArray *array = [NSMutableArray array];
//        for (int i=0; i<self.topicOptionArr.count+1000; i++) {
//            UIButton *button = [self viewWithTag:i];
//            if (button.selected) {
//                NSDictionary *tempDic = self.topicOptionArr[i-1000];
//
//                [array addObject:tempDic[@"id"]];
//                if ([self.subjectType isEqualToString:@"select"]) {
//                    [dic setValue:tempDic[@"linkValue"] forKey:@"linkValue"];
//                    break;
//                }
//            }
//        }
//
//        [dic setValue:array forKey:@"solutionId"];
//        [dic setValue:self.topicIndex forKey:@"topicIndex"];
//        [dic setValue:self.topTopicIndex forKey:@"topTopicIndex"];
//    }
    
    if (self.topBlcok) {
        self.topBlcok(dic);
    }
}

- (IBAction)nextClick:(id)sender {
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];  //数据都存在这个字典里
    NSMutableArray *array = [NSMutableArray array];
    for (int i=0; i<self.topicOptionArr.count+1000; i++) {
        UIButton *button = [self viewWithTag:i];
        if (button.selected) {
            NSDictionary *tempDic = self.topicOptionArr[i-1000];
            
            [array addObject:tempDic[@"id"]];
            if ([self.subjectType isEqualToString:@"select"]) {
                [dic setValue:tempDic[@"linkValue"] forKey:@"linkValue"];
                break;
            }
        }
    }
    
    [dic setValue:array forKey:@"solutionId"];
    [dic setValue:self.topicIndex forKey:@"topicIndex"];
    [dic setValue:self.topTopicIndex forKey:@"topTopicIndex"];
    
    if (array.count > 0) {
        if (self.nextBlcok) {
            self.nextBlcok(dic);
        }
    }else{
        [GXToast showText:@"请选择相应选项"];
    }
    
}

- (IBAction)buttonClick:(id)sender {
    NSLog(@"点击了button");
    if ([self.subjectType isEqualToString:@"select"]) {
        for (int i=1000; i<1000+self.topicOptionArr.count; i++) {
            UIButton *button = [self viewWithTag:i];
            if (sender == button) {
                button.selected = YES;
                button.backgroundColor = [UIColor colorWithRed:241/255.0 green:247/255.0 blue:253/255.0 alpha:1.0];
            }else{
                button.selected = NO;
                button.backgroundColor = [UIColor colorWithRed:248/255.0 green:248/255.0 blue:248/255.0 alpha:1.0];
            }
        }
        [self nextClick:nil];
    }
    else if ([self.subjectType isEqualToString:@"select_many"]) {
        UIButton *button = sender;
        if (!button.selected) {
            button.selected = YES;
            button.backgroundColor = [UIColor colorWithRed:241/255.0 green:247/255.0 blue:253/255.0 alpha:1.0];
        }else{
            button.selected = NO;
            button.backgroundColor = [UIColor colorWithRed:248/255.0 green:248/255.0 blue:248/255.0 alpha:1.0];
        }
    }
    
    if (_lastValue) {
        [self lastClick];
    }
}

- (void)lastClick{
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    NSMutableArray *array = [NSMutableArray array];
    for (int i=0; i<self.topicOptionArr.count+1000; i++) {
        UIButton *button = [self viewWithTag:i];
        if (button.selected) {
            NSDictionary *tempDic = self.topicOptionArr[i-1000];
            
            [array addObject:tempDic[@"id"]];
            if ([self.subjectType isEqualToString:@"select"]) {
                [dic setValue:tempDic[@"linkValue"] forKey:@"linkValue"];
                break;
            }
        }
    }
    
    [dic setValue:array forKey:@"solutionId"];
    [dic setValue:self.topicIndex forKey:@"topicIndex"];
    [dic setValue:self.topTopicIndex forKey:@"topTopicIndex"];
    
    if (self.lastBlcok) {
        self.lastBlcok(dic);
    }
}

- (void)setTopicOptionArr:(NSArray *)topicOptionArr{
    if (_topicOptionArr != topicOptionArr) {
        _topicOptionArr = topicOptionArr;
        
        for (int i=0; i<20; i++) {
            UIButton *button = [self viewWithTag:i+1000];
            [button removeFromSuperview];
        }
        
        for (int i=0; i<topicOptionArr.count; i++) {
            UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
            button.frame = CGRectMake(30, CGRectGetMaxY(self.mianTextView.frame)+(70)*i+20, self.frame.size.width-60, 60);
            button.backgroundColor = [UIColor colorWithRed:248/255.0 green:248/255.0 blue:248/255.0 alpha:1.0];
            button.layer.cornerRadius = 16;
            button.layer.masksToBounds = YES;
            button.tag = 1000+i;   //tag
            button.titleLabel.textAlignment = NSTextAlignmentLeft;
            button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
            button.titleEdgeInsets = UIEdgeInsetsMake(0, 20, 0, 0);
            [button setTitle:topicOptionArr[i][@"title"] forState:0];
            [button setTitleColor:[UIColor colorWithRed:51/255.0 green:51/255.0 blue:51/255.0 alpha:1.0] forState:0];
            [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
            [self.scrollView addSubview:button];
            
            button.selected = NO;
            NSArray *linkValueArr = self.seletedDic[@"solutionId"];
            if ([linkValueArr containsObject:topicOptionArr[i][@"id"]]) {  //如果包含
                button.selected = YES;
                button.backgroundColor = [UIColor colorWithRed:241/255.0 green:247/255.0 blue:253/255.0 alpha:1.0];
            }
            
            if (i == (topicOptionArr.count-1)) {
                self.gao.constant = CGRectGetMaxY(button.frame)+130;
            }
        }
    }
}


-(void)textViewDidChange:(UITextView *)textView{
    float textViewHeight =  [textView sizeThatFits:CGSizeMake(textView.frame.size.width, MAXFLOAT)].height;
    CGRect frame = textView.frame;
    frame.size.height = textViewHeight;
    textView.frame = frame;
    _textViewGao.constant = textView.frame.size.height;
}



@end
